package com.quasar.hpatient.view;

import android.annotation.TargetApi;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.operationmodel.SynchronizationData;
import com.quasar.hpatient.utils.LogAndToastUtil;
import com.quasar.hpatient.utils.SharedPreferencesUtil;
import com.quasar.hpatient.utils.runtimepermissions.PermissionsManager;
import com.quasar.hpatient.utils.runtimepermissions.PermissionsResultAction;
import com.quasar.hpatient.view.adapter.GuideViewPagerAdapter;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@EActivity(R.layout.activity_welcome_guide)
public class WelcomeGuideActivity extends BaseActivity implements View.OnClickListener {

    private List<View> views;
    Button startBtn;
    private GuideViewPagerAdapter adapter;
    // 引导页图片资源
    private static final int[] pics = {R.layout.guide_view1,
            R.layout.guide_view2, R.layout.guide_view3};
    // 底部小点图片
    private ImageView[] dots;
    // 记录当前选中位置
    private int currentIndex;

    @ViewById(R.id.vp_guide)
    ViewPager vp;
    @ViewById(R.id.ll)
    LinearLayout ll;

    @AfterViews
    void init() {
        requestPermissions();
        initView();
//        initData();
        views = new ArrayList<View>();
        // 初始化引导页视图列表
        for (int i = 0; i < pics.length; i++) {
            View view = LayoutInflater.from(this).inflate(pics[i], null);

            if (i == pics.length - 1) {
                startBtn = (Button) view.findViewById(R.id.btn_enter);
                startBtn.setTag("enter");
                startBtn.setOnClickListener(this);
            }

            views.add(view);

        }
        adapter = new GuideViewPagerAdapter(views);
        vp.setAdapter(adapter);
        vp.addOnPageChangeListener(new PageChangeListener());
        initDots();
    }

    //控制底部小点
    private void initDots() {
        dots = new ImageView[pics.length];
        // 循环取得小点图片
        for (int i = 0; i < pics.length; i++) {
            // 得到一个LinearLayout下面的每一个子元素
            dots[i] = (ImageView) ll.getChildAt(i);
            dots[i].setEnabled(false);// 都设为灰色
            dots[i].setOnClickListener(WelcomeGuideActivity.this);
            dots[i].setTag(i);// 设置位置tag，方便取出与当前位置对应
        }
        currentIndex = 0;
        dots[currentIndex].setEnabled(true); // 设置为白色，即选中状态
    }

    /**
     * 设置当前view
     *
     * @param position
     */
    private void setCurView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }
        vp.setCurrentItem(position);
    }

    /**
     * 设置当前指示点
     *
     * @param position
     */
    private void setCurDot(int position) {
        if (position < 0 || position > pics.length || currentIndex == position) {
            return;
        }
        dots[position].setEnabled(true);
        dots[currentIndex].setEnabled(false);
        currentIndex = position;
    }


    @Override
    public void onClick(View v) {
        if (v.getTag().equals("enter")) {
            enterMainActivity();
            return;
        }
        int position = (Integer) v.getTag();
        setCurView(position);
        setCurDot(position);
    }

    private void enterMainActivity() {
        SplashActivity_.intent(this).start();
        SharedPreferencesUtil.putBoolean(WelcomeGuideActivity.this, SharedPreferencesUtil.FIRST_OPEN, false);
        finish();

    }

    @Override
    protected void initView() {
        writeDataBase();
    }

    @Override
    protected void initData() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                SynchronizationData.Durationinfo();//不适症状
                SynchronizationData.Troubleinfo();//不适时长
                SynchronizationData.HospitalUpdate();//医院
                SynchronizationData.Aerainfo();//地理信息
                SynchronizationData.Inspectiondetailinfo();//查询检查单详情
                SynchronizationData.checklist();//检查单信息
                SynchronizationData.InspectionItem();//查询检查单检查项目信息
                SynchronizationData.checklistType();//日常检查单分类信息

                SynchronizationData.GetDiagnoseinfo();//诊断信息

                SynchronizationData.DosePeriodData();//查询服药周期信息
                SynchronizationData.DoseModeDB();//服药方式
                SynchronizationData.DrugsDose();//查询药品剂量名称
                SynchronizationData.Medicineinfo();//查询药品信息
                SynchronizationData.Medicinetype();//查询药品类型信息
                SynchronizationData.DayRecord();//获取日常检查项
                SynchronizationData.Dimdailyiteminfo();//查询检查项目量纲名称信息
                SynchronizationData.BloodType();//血型信息
                SynchronizationData.Kidneysourcetype();//肾来源
                SynchronizationData.PositionData();//职位信息
                SynchronizationData.Departmentinfo();//科室信息
                SynchronizationData.VersionUpdate();//版本信息

            }
        }).start();

    }

    private class PageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int position) {

        }

        @Override
        public void onPageScrolled(int position, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int position) {
            // 设置底部小点选中状态
            setCurDot(position);
        }

    }
    String path = ConstantManger.DARABASEDBPATH;

    public boolean writeDataBase() {
        File file = new File(path);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return true;
            } else
                return false;
        }
        return true;
    }


    //判断23以上的动态申请权限
    @TargetApi(23)
    private void requestPermissions() {
        PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, new PermissionsResultAction() {
            @Override
            public void onGranted() {
//				Toast.makeText(MainActivity.this, "All permissions have been granted", Toast.LENGTH_SHORT).show();
                LogAndToastUtil.toast(WelcomeGuideActivity.this,"开启权限","");
            }

            @Override
            public void onDenied(String permission) {
                LogAndToastUtil.toast(WelcomeGuideActivity.this,"开启权限","");
            }
        });
    }

    //权限借口回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
    }
}
